package ext.vpde.cadvaulting.util;

import wt.content.ContentHelper;
import wt.epm.*;
import wt.epm.structure.*;
import wt.fc.*;
import wt.query.*;
import wt.util.WTProperties;
import java.util.*;
import wt.content.*;
import java.net.URL;
import com.infoengine.SAK.IeService;
import com.infoengine.SAK.Task;
import com.infoengine.au.NamingService;
import java.io.*;

public class ProcessDrawing {
	
	public static String ADAPTER       = null;
	public static String RESOURCE      = null;
    public static String NAMINGSERVICE = null;
    public static String USERID        = null;
    public static String USERPW        = null;
    public static String TEMPDIR       = null;
    public static String PROEDIR       = null;
    public static String WTHOME        = null;
	
	public static void createPDF(EPMDocument doc, boolean addWatermark) throws Exception {
		
		WTProperties wtProps = WTProperties.getLocalProperties();
	    ADAPTER       = wtProps.getProperty("wt.federation.ie.VMName");
	    RESOURCE      = wtProps.getProperty("wt.federation.ie.propertyResource");
	    NAMINGSERVICE = wtProps.getProperty("wt.federation.ie.namingService");
	    USERID        = wtProps.getProperty("ext.vpde.cadvaulting.userid");
	    USERPW        = wtProps.getProperty("ext.vpde.cadvaulting.userpw");
	    PROEDIR       = wtProps.getProperty("proe.dir");
	    WTHOME        = wtProps.getProperty("wt.home");
	   
		long timeStamp = Calendar.getInstance().getTimeInMillis();
		TEMPDIR = wtProps.getProperty("wt.temp") + "\\" + String.valueOf(timeStamp);
		
		// First download the drawing content
		String dwgName = doc.getCADName();
		String urlStr = getURL(doc);
		//getContent(urlStr,dwgName);
		
		// Second, get the related items and download them
		QuerySpec qs   = new QuerySpec(EPMDocument.class);
		qs.appendWhere(new SearchCondition(EPMDocument.class,"master>name",  "=", doc.getName()));
		qs.appendOrderBy(EPMDocument.class,"thePersistInfo.theObjectIdentifier.id",true);
		QueryResult qr = EPMStructureHelper.service.navigateReferences(doc, null, true);
		while (qr.hasMoreElements()) {
			EPMDocumentMaster epmdocmaster = (EPMDocumentMaster) qr.nextElement();
			System.out.println("\nFOUND: " + epmdocmaster.getName());
			QueryResult qi = EPMDocumentHelper.service.findReferencedIterations(epmdocmaster);
			while (qi.hasMoreElements()) {
				EPMDocument epmdoc = (EPMDocument)qi.nextElement();
				String cadName = epmdoc.getCADName();
				if (epmdoc.isLatestIteration()) {
					urlStr = getURL(epmdoc);
					//getContent(urlStr.toString(),cadName);
				}
			}				
		}
		
		// Third, generate the batch file
		//File batchFile = GeneratePDF.createBatchFile(TEMPDIR, dwgName);
		
		//Fourth, generate the ps file using pro batch then add the watermark if checked in
		//GeneratePDF.generatePS(PROEDIR, batchFile.getAbsolutePath());
		String psFileName = dwgName.substring(0,dwgName.indexOf(".")) + ".ps";
		File psFile = new File(TEMPDIR, psFileName);
		
		//if (addWatermark) {
			//GeneratePDF.insertWatermark(psFile,"%%EndPageSetup", new File(WTHOME + "\\NR_watermark.txt") );
		//}
		
		// Lastly, generate the pdf
		//GeneratePDF.generatePDF(psFile);

	}
	
	public static String getURL(EPMDocument doc) throws Exception {
		ContentHolder ch = ContentHelper.service.getContents(doc);
		Vector vector = ContentHelper.getContentListAll(ch);
		ApplicationData ad = (ApplicationData)vector.firstElement();
		URL url = ContentHelper.getDownloadURL(doc, ad);
		return url.toString();
	}
	
	public static void getContent(String urlstr, String cadName) throws Exception {

		Task t = new Task("ext/vpde/cadvaulting/GetContent.xml");
		NamingService.setVMName ("remote");
		NamingService.newInstance (NAMINGSERVICE,RESOURCE,null);
		IeService ie = new IeService();
		ie.setEnableExceptions ( true );
		ie.setCredentials(USERID, USERPW);
		Vector processors=new Vector();
		processors.add(ADAPTER);
		t.setProcessors(processors);
		t.setUsername(USERID);
		t.setService (ie);
		t.addParam("URL",urlstr);
		t.addParam("NAME",cadName);
		t.addParam("DIR",TEMPDIR);
		t.addParam("USERID",USERID);
		t.addParam("USERPW",USERPW);
		t.invoke();
	}
}
